package com.bingo.generate.cli.Cmds;


import com.bingo.generate.comm.entity.ColInfo;
import com.bingo.generate.comm.util.FmUtil;
import com.bingo.generate.comm.util.StringHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * ClassName    生成下划线风格的vo实体类
 * Description
 * Create by lc
 * Date 2021/7/19 13:37
 */
public class JavaToPhpVoCodeCmd {

    //  返回两张表有数据差异的列
    public static void createEntityFtl(String entityName,List<ColInfo> list){
        String tbName=null;
        if(entityName==null){
            entityName=list.get(0).getTableName();
            //entityName= StringHelper.underlineToCamel(entityName)+"Info";
            entityName= StringHelper.underlineToCamel(entityName);
            entityName=StringHelper.cfLetter(entityName);
            //tbName=list.get(0).getTableName();
        }
        tbName=list.get(0).getTableName();
        Map<String,Object> param=new HashMap<>();
        param.put("modelName",entityName);
        param.put("tbName",tbName);
        param.put("cols",list);
        //  根据列名进行分组
        FmUtil.printComm("javaToPhpVoTipsAdd.ftl",param);
    }



    public static String getType(ColInfo col){
        String type=col.getType();
        if ("int".equals(type)||"tinyint".equals(type)){
            if(col.getColName().toLowerCase().endsWith("id")){
                return "String";
            }
            return "Integer";
        }
        if("varchar".equals(type)){
            return "String";
        }
        if("datetime".equals(type)){
            return "LocalDateTime";
        }
        if("timestamp".equals(type)){
            return "Date";
        }
        if ("bigint".equals(type)||"bigint".equals(type)){
            return "Long";
        }
        throw new RuntimeException("该类型未曾处理"+col.getType());
    }

    //  @ApiModelProperty(value = "")
    public static String getAnnotations(ColInfo col){
        StringBuilder strBud=new StringBuilder();
        strBud.append("").append("@ApiModelProperty(value = \""+col.getComment()+"\")");
        if("id".equals(col.getColName())){
            //strBud.append("\n\t").append("@TableId(type = IdType.AUTO)");
        }
        /*else{
            strBud.append("\n\t").append("@TableField(value = \""+col.getColName()+"\")");
        }*/
        if(strBud.length()>0){
            strBud.append("\n\t");
        }
        return strBud.toString();
    }

    //  根据col变成常量
    public static String getSet(ColInfo col){
        String underline=StringHelper.cfLetter(col.getColName());
        String underline2="this.set"+underline;
        String camel=StringHelper.cfLetter(StringHelper.underlineToCamel(col.getColName()));
        String getMethod="entity.get"+camel+"()";
        StringBuilder strBud=new StringBuilder();
        strBud.append(String.format("if(%s!=null)",getMethod)).append("{\n");
        strBud.append("\t\t\tthis.set").append(underline);
        strBud.append("(entity.get").append(camel).append("());");
        strBud.append("\n\t\t}");
        return strBud.toString();
    }
    
    public static String toFieldName(String colName){
        return StringHelper.underlineToCamel(colName);
    }

    public static String getSqlColStr(List<ColInfo> cols){
        List<String> list=cols.stream().map(ColInfo::getColName).collect(Collectors.toList());
        return String.join(",",list);
    }

}
